#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/1/15 14:41
# @Author : Yoin

import random
import time
age_bracket = ["18-25", "25-35", "35-45", "45-55"]
platform = [
    "platform_1", "platform_2", "platform_3", "platform_4", "platform_5", "platform_6", "platform_7", "platform_8",
    "platform_9", "platform_10"
]
p_platform = [0.15, 0.08, 0.02, 0.1, 0.12, 0.08, 0.09, 0.1, 0.22, 0.04]
type = ["economy", "comfortable", "business"]
distance = ["0-10", "10-20", "20-30", "30-40", ">=40"]
region = ["beijing", "shanghai", "shenzhen", "guangzhou", "chengdu", "hangzhou", "nanjing", "jinan", "dalian", "tianjin"]
p_region = [0.17, 0.18, 0.12, 0.11, 0.1, 0.09, 0.08, 0.05, 0.03, 0.07]
def acquire_region():
    return random.choices(region, weights=p_region)[0]
def acquire_age():
    return random.choices(age_bracket, weights=[1, 4, 3, 2])[0]
def choices_platform():
    return random.choices(platform, weights=p_platform)[0]
def acquire_type(platform):
    if p_platform[platform.index(platform)] >= 0.1:
        a_type = random.choices(type, weights=[6, 3, 1])[0]
    elif 0.05 <= p_platform[platform.index(platform)] < 0.1:
        a_type = random.choices(type, weights=[5, 3.5, 1.5])[0]
    elif p_platform[platform.index(platform)] < 0.05:
        a_type = random.choices(type, weights=[4.5, 3.5, 2])[0]
    return a_type
def acquire_distance(age):
    if age == age_bracket[0]:
        a_distance = random.choices(distance, weights=[6, 1.5, 1.2, 1, 0.3])[0]
    elif age == age_bracket[1]:
        a_distance = random.choices(distance, weights=[1.5, 1.5, 2, 3, 2])[0]
    elif age == age_bracket[2]:
        a_distance = random.choices(distance, weights=[1.5, 2, 2, 2.5, 2])[0]
    else:
        a_distance = random.choices(distance, weights=[1, 1.5, 2, 2.5, 3])[0]
    return a_distance
def acquire_score(c_platform):
    if p_platform[platform.index(c_platform)] >= 0.2:
        a_score = format(random.uniform(8, 10), '.1f')
    elif 0.1 <= p_platform[platform.index(c_platform)] < 0.2:
        a_score = format(random.uniform(7, 9.5), '.1f')
    else:
        a_score = format(random.uniform(6.5, 9.5), '.1f')
    return a_score
def generator_log(count=10):
    time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    f = open("/var/log/car.log", "a+")
    while count >= 1:
        t_region = acquire_region()
        t_age = acquire_age()
        t_platform = choices_platform()
        t_type = acquire_type(t_platform)
        t_distance = acquire_distance(t_age)
        t_score = acquire_score(t_platform)
        query_log = "{region}\t{platform}\t{age_bracket}\t{type}\t{distance}\t{score}\t{date}\t".format(region=t_region, platform=t_platform, age_bracket=t_age,                                                                                                        type=t_type, distance=t_distance, score=t_score, date=time_str)
        f.write(query_log + "\n")
        #print(query_log)
        count = count - 1
if __name__ == '__main__':
    while True:
        generator_log(random.randint(10,30))
        time.sleep(random.randint(1,10))